<template>
  <div class='user-table'>
    <el-dialog
      :title='!userForm.id ? '新增' : '修改''
      :visible.sync='userAddDialogVisible'
       width='50%' :center='true'>
      <el-form :model='userForm' ref='userForm' label-width='80px'>
          <el-form-item prop='username' label='用户名' size='mini'>
            <el-input v-model='userForm.username' placeholder='用户名'></el-input>
          </el-form-item>
        <el-form-item prop='name' label='用户昵称' size='mini'>
          <el-input v-model='userForm.nickname' placeholder='用户昵称'></el-input>
        </el-form-item>
        <el-form-item prop='delflag' label='状态' size='mini'>
          <el-radio-group v-model='userForm.delflag'>
            <el-radio :label='0'>正常</el-radio>
            <el-radio :label='1'>删除</el-radio>
            <el-radio :label='1'>锁定</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop='sex' label='性别' size='mini'>
          <el-radio-group v-model='userForm.sex'>
            <el-radio :label='0'>女</el-radio>
            <el-radio :label="1">男</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="email" label="邮箱" size="mini">
          <el-input v-model="userForm.email" placeholder="邮箱"></el-input>
        </el-form-item>
        <el-form-item prop="birth" label="生日" size="mini">
          <el-date-picker type="date" placeholder="选择日期" v-model="userForm.birth" style="width: auto"></el-date-picker>
        </el-form-item>
        <el-form-item prop="headImgUrl" label="头像" size="mini">
          <singlePicUpload v-model="userForm.headImgUrl" @input="inputHandler" ></singlePicUpload>
        </el-form-item>
        <el-form-item prop="location" label="地区" size="mini">
          <el-input v-model="userForm.location" placeholder="地区"></el-input>
        </el-form-item>
        <el-form-item prop="description" label="描述" size="mini">
          <el-input v-model="userForm.description" placeholder="地区"></el-input>
        </el-form-item>
        <el-form-item prop="introduction" label="介绍" size="mini">
          <el-input v-model="userForm.introduction" placeholder="介绍" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" type="warning" @click="userAddDialogVisible=false">取消</el-button>
        <el-button size="mini"  type="primary" @click="addUser">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import singlePicUpload from '@/components/upload/singlePicUpload'
export default {
  name: 'user-add-or-update',
  components: {
    singlePicUpload
  },
  data () {
    return {
      userAddDialogVisible: false,
      userForm: {
        id: '',
        username: '',
        nickname: '',
        email: '',
        phone: '',
        type: '',
        sex: '',
        birth: '',
        headImgUrl: '',
        location: '',
        address: '',
        description: '',
        introduction: ''
      }
    }
  },
  methods: {
    inputHandler (url) {
      console.log(url)
      console.log(1111)
      this.userForm.pic = url
    },
    init (id) {
      console.log(id)
      this.userForm.id = id || 0
      this.userAddDialogVisible = true
      this.$nextTick(() => {
        this.$refs['userForm'].resetFields()
        if (this.userForm.id) {
          this.$api.getUserById({
            params: {id: id}
          }).then((res) => {
            console.log(res.data)
            if (res.data && res.data.code === 200) {
              console.log(22222)
              this.userForm.username = res.data.data.username
              this.userForm.nickname = res.data.data.nickname
              this.userForm.headImgUrl = res.data.data.headImgUrl
              this.userForm.sex = res.data.data.sex
              this.userForm.birth = res.data.data.birth
              this.userForm.email = res.data.data.email
              this.userForm.location = res.data.data.location
              this.userForm.introduction = res.data.data.introduction
              this.userForm.phone = res.userForm.phone
              this.userForm.type = res.userForm.type
              this.userForm.address = res.userForm.address
              this.userForm.description = res.userForm.description
            }
          })
        }
      })
    },
    addUser () {
      let that = this
      let birth = this.userForm.birth
      if (birth) {
        // birth = birth.getFullYear() + '-' + (birth.getMonth() + 1) + '-' + birth.getDate()
        birth = new Date(birth).getTime()
      }
      let params = {
        username: this.userForm.username,
        sex: this.userForm.sex,
        birth: birth,
        headImgUrl: this.userForm.headImgUrl,
        location: this.userForm.location,
        introduction: this.userForm.introduction,
        nickname: this.userForm.nickname,
        email: this.userForm.email,
        phone: this.userForm.phone,
        type: this.userForm.type,
        address: this.userForm.address,
        description: this.userForm.description
      }
      if (!this.userForm.id) {
        this.$api.addUser(params).then(res => {
          console.log(res)
          if (res.data.code === 200) {
            that.userAddDialogVisible = false
            that.$emit('refreshDataList')
          } else {
            console.log(res.data)
          }
        }).catch(error => {
          console.log(error)
        })
      } else {
        params.id = this.userForm.id
        this.$api.updateUser(params).then(res => {
          console.log(res)
          if (res.data.code === 200) {
            that.userAddDialogVisible = false
            that.$emit('refreshDataList')
          } else {
            console.log(res.data)
          }
        }).catch(error => {
          console.log(error)
        })
      }
    }
  }
}
</script>

<style scoped>
  .user-table{
    margin: auto;
    width: 80%;
  }
  .user-img{
    width: 100px;
    height: 100px;
    border-radius: 5px;
    overflow: hidden;
  }
  body .el-table th.gutter{
    display: table-cell!important;
  }

</style>
